package com.tian.web.admin.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.oscache.util.StringUtil;
import com.tian.core.action.JqGridBaseAction;
import com.tian.core.bean.Condition;
import com.tian.core.bean.ConditionOperate;
import com.tian.core.bean.ConditionType;
import com.tian.core.bean.Pages;
import com.tian.core.entity.Depart;
import com.tian.core.entity.Group;
import com.tian.core.entity.Ztree;
import com.tian.core.entity.Ztreegroup;
import com.tian.web.admin.service.DepartService;
import com.tian.web.admin.service.GroupService;
import com.tian.web.admin.service.ZtreeGroupService;
import com.tian.web.admin.service.ZtreeService;

@Namespace("/admin")
@Results({
	@Result(type = "dispatcher",name="ztree",location="/pages/admin/ztree_list.jsp"),
})
public class ZtreeAction extends JqGridBaseAction{

	private static final long serialVersionUID = -3210254182108227528L;
	@Resource
	private ZtreeService ztreeService;
	@Resource
	private GroupService groupService;
	@Resource
	private ZtreeGroupService zgService;
	@Resource
	private DepartService departService;
	
	private List<Ztree> gridModel;
	private List<Group> groupList;
	private List<Depart> departList;
	
	private Ztree ztree;
	private Integer groupid;
	private Integer pid;
	private String isPm;
	@Override
	public String execute() throws Exception {
		
		return "ztree";
	}
	
	public String list() throws Exception{
		
		return this.refreshGridModel();
	}
	
    public String delete() throws Exception {
/*    	String[] ids = null;
		if(!StringUtil.isEmpty(this.id)){
			ids = id.split(",");
		}*/
    	if(StringUtil.isEmpty(this.id)){
    		return ERROR;
    	}
    	//ztree = ztreeService.get(Integer.parseInt(id));
    	//删除与用户组关联
		List<Ztreegroup> zgs = zgService.getList("ztree.id", Integer.parseInt(id));
		for(Ztreegroup zg : zgs){
			zgService.delete(zg);
		}
		ztreeService.delete(Integer.parseInt(id));
    	return ajax("ajax_ok");
    }
    
    public String addOrUpdate() throws Exception {
    	if(id!=null){//编辑
    		ztree = ztreeService.get(Integer.parseInt(id));
    	}else{
    		//新增
    		ztree = new Ztree();
    	}
    	gridModel = ztreeService.getAll(Ztree.class);
    	groupList = groupService.getAll(Group.class);
    	departList = departService.getAll(Depart.class);
    	return "update";
    }
    
    public String save() throws Exception {
    	//Group g = groupService.get(groupid);

    	if(this.isPm.equals("1")){//是父级菜单
    		ztree.setUrl("");
    		ztree.setIcon("");
    	}else if(this.isPm.equals("0")){
    		ztree.setIcon("css/zTreeStyle/img/diy/2.png");
    	}
    	
    	if(ztree.getId()!= null){//编辑
    		Ztree zt = ztreeService.get(ztree.getId());
    		zt.setName(ztree.getName());
    		zt.setTarget(ztree.getTarget());
    		zt.setpId(pid);
    		zt.setUrl(ztree.getUrl());
        	zt.setIcon(ztree.getIcon());
        	zt.setUrl(ztree.getUrl());
        	ztreeService.update(zt);
        	
/*    		Ztreegroup zg = zgService.get("ztree.id", zt.getId());
    		zg.setGroup(g);
    		zgService.update(zg);*/
    		
    	}else{//新增
    		ztree.setpId(pid);
    		//ztree.setIcon("css/zTreeStyle/img/diy/2.png");
    		ztree.setOpen(true);
    		ztree.setChecked(false);
    		Ztree node = ztreeService.save(ztree);
    		
/*    		Ztreegroup zg = new Ztreegroup();
    		zg.setCreatedate(new Date());
    		zg.setZtree(node);
    		zg.setGroup(g);
    		zgService.save(zg);*/
    		
    	}
    	return "ztree";
    }

	@Override
	public String refreshGridModel() {
		this.record = ztreeService.getTotalCount(Ztree.class);
		Pages pages = new Pages();		
    	pages.setPage(page);
    	pages.setTotalNum(record);
    	pages.setPerPageNum(rows);
    	pages.executeCount();
    	total = pages.getAllPage();
    	
    	if(!StringUtil.isEmpty(this.sidx) && !StringUtil.isEmpty(this.sord)){
    		Condition condition1 = new Condition();
        	condition1.setName(this.sidx);
    		//condition1.setValue(Condition.DESC);
    		condition1.setValue(this.sord);
    		condition1.setType(ConditionType.ORDER);
    		conditions.add(condition1);
    	}
    	//只实现equal
    	if(this.search && !StringUtil.isEmpty(this.searchField)
    			&& !StringUtil.isEmpty(this.searchOper)&& !StringUtil.isEmpty(this.searchString)){
    		Condition condition2 = new Condition();
    		condition2.setName(this.searchField);
    		//condition1.setValue(Condition.DESC);
    		condition2.setValue(this.searchString);
    		condition2.setType(ConditionType.PROPERTY);
    		condition2.setOperate(ConditionOperate.EQUAL);
    		conditions.add(condition2);
    	}
    		
    	pageList = ztreeService.findByPages(conditions, pages);
		pages = pageList.getPages();
		
        this.rows = pages.getPerPageNum();
        this.record = pages.getTotalNum();
        this.page = pages.getPage();
        
        gridModel = (List<Ztree>)pageList.getObjectList();
        
/*		for(Ztree z :gridModel){
			z.setGroup(null);
		}*/
		System.out.println("rows:"+rows+" page:"+ page + " total:" + total + " record:" + record);
		return SUCCESS;
	}

	public List<Ztree> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<Ztree> gridModel) {
		this.gridModel = gridModel;
	}

	public Ztree getZtree() {
		return ztree;
	}

	public void setZtree(Ztree ztree) {
		this.ztree = ztree;
	}

	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}

	public Integer getGroupid() {
		return groupid;
	}

	public void setGroupid(Integer groupid) {
		this.groupid = groupid;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String getIsPm() {
		return isPm;
	}

	public void setIsPm(String isPm) {
		this.isPm = isPm;
	}
	
}
